home *** CD-ROM | disk | FTP | other *** search
/ NetObjects Fusion 7 / Fusion7.iso / NetObjects Fusion / data1.cab / Language_Resource_-_English / Components / RotatingPictureComp / RotatingPicture.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-10-18  |  8.7 KB  |  253 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Dimension;
  5. import java.awt.Event;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.awt.MediaTracker;
  9. import java.awt.Rectangle;
  10. import java.awt.image.ImageObserver;
  11. import java.net.URL;
  12.  
  13. public class RotatingPicture extends Applet implements Runnable {
  14.    Thread m_RotatingPicture = null;
  15.    static final String STRING_Error_forming_URL = new String("Error forming URL for background image");
  16.    static final String STRING_Image_loading_interrupted = new String("Background image loading interrupted");
  17.    static final String STRING_Unable_to_load_images = new String("Unable to load images over Windows network!");
  18.    static final String STRING_Loading_images = new String("Loading images...");
  19.    static final String STRING_Error_loading_images = new String("Error loading images!");
  20.    static final String STRING_Bad_URL = new String("Bad URL:");
  21.    private Graphics m_Graphics;
  22.    private Image[] m_Images;
  23.    private int m_nCurrImage;
  24.    private int m_nImgWidth = 0;
  25.    private int m_nImgHeight = 0;
  26.    private boolean m_fAllLoaded = false;
  27.    private boolean NetworkFiles = false;
  28.    private boolean didClick;
  29.    private int pauseTime;
  30.    private int numImages;
  31.    private String[] imageNames;
  32.    private String[] imageURLs;
  33.    protected Color backgroundColor = null;
  34.    protected Image backgroundImage = null;
  35.    protected int xOffset;
  36.    protected int yOffset;
  37.  
  38.    public String getAppletInfo() {
  39.       return "Name: RotatingPicture\r\nAuthor: Alex Kogon\r\n";
  40.    }
  41.  
  42.    public void init() {
  43.       this.xOffset = Integer.parseInt(((Applet)this).getParameter("X Position"));
  44.       this.yOffset = Integer.parseInt(((Applet)this).getParameter("Y Position"));
  45.       this.didClick = false;
  46.       this.pauseTime = 1000 * Integer.parseInt(((Applet)this).getParameter("Pause Time"));
  47.       this.numImages = Integer.parseInt(((Applet)this).getParameter("Number of Images"));
  48.       this.imageNames = new String[this.numImages];
  49.       this.imageURLs = new String[this.numImages];
  50.  
  51.       for(int cnt = 0; cnt < this.numImages; ++cnt) {
  52.          this.imageNames[cnt] = ((Applet)this).getParameter("Image " + (cnt + 1));
  53.          if (this.imageNames[cnt] != null && this.imageNames[cnt].startsWith("file://///")) {
  54.             this.NetworkFiles = true;
  55.          }
  56.  
  57.          this.imageURLs[cnt] = ((Applet)this).getParameter("URL for Image " + (cnt + 1));
  58.          if (this.imageURLs[cnt] != null) {
  59.             if (this.imageURLs[cnt].startsWith(".")) {
  60.                String theBase = ((Applet)this).getDocumentBase().toExternalForm();
  61.                int lastSlash = theBase.lastIndexOf(47);
  62.                this.imageURLs[cnt] = theBase.substring(0, lastSlash + 1) + this.imageURLs[cnt];
  63.             } else if (this.imageURLs[cnt].compareTo("javascript:void(0)") == 0) {
  64.                this.imageURLs[cnt] = null;
  65.             } else if (!this.imageURLs[cnt].startsWith("cid:") && !this.imageURLs[cnt].startsWith("lifn:") && !this.imageURLs[cnt].startsWith("java:") && !this.imageURLs[cnt].startsWith("irc:") && !this.imageURLs[cnt].startsWith("IOR:") && !this.imageURLs[cnt].startsWith("ilu:") && !this.imageURLs[cnt].startsWith("https:") && !this.imageURLs[cnt].startsWith("http:") && !this.imageURLs[cnt].startsWith("hdl:") && !this.imageURLs[cnt].startsWith("gopher:") && !this.imageURLs[cnt].startsWith("ftp:") && !this.imageURLs[cnt].startsWith("finger:") && !this.imageURLs[cnt].startsWith("file:") && !this.imageURLs[cnt].startsWith("data:") && !this.imageURLs[cnt].startsWith("clsid:") && !this.imageURLs[cnt].startsWith("md5:") && !this.imageURLs[cnt].startsWith("mailserver:") && !this.imageURLs[cnt].startsWith("mailto:") && !this.imageURLs[cnt].startsWith("mid:") && !this.imageURLs[cnt].startsWith("news:") && !this.imageURLs[cnt].startsWith("nntp:") && !this.imageURLs[cnt].startsWith("path:") && !this.imageURLs[cnt].startsWith("prospero:") && !this.imageURLs[cnt].startsWith("service:") && !this.imageURLs[cnt].startsWith("shttp") && !this.imageURLs[cnt].startsWith("snews") && !this.imageURLs[cnt].startsWith("STANF:") && !this.imageURLs[cnt].startsWith("telnet:") && !this.imageURLs[cnt].startsWith("vemmi:") && !this.imageURLs[cnt].startsWith("wais:") && !this.imageURLs[cnt].startsWith("whois++:")) {
  66.                this.imageURLs[cnt] = "http://" + this.imageURLs[cnt];
  67.             }
  68.          }
  69.       }
  70.  
  71.       if (((Applet)this).getParameter("BackgroundColor") != null) {
  72.          int backgroundcolor = Integer.parseInt(((Applet)this).getParameter("BackgroundColor"));
  73.          backgroundcolor = this.convertBGRtoRGB(backgroundcolor);
  74.          this.backgroundColor = new Color(backgroundcolor);
  75.          ((Component)this).setBackground(this.backgroundColor);
  76.       } else if (((Applet)this).getParameter("BackgroundImage") != null) {
  77.          String backgroundimage = this.modifyStringContext(((Applet)this).getParameter("BackgroundImage"));
  78.  
  79.          try {
  80.             this.backgroundImage = ((Applet)this).getImage(new URL(backgroundimage));
  81.          } catch (Exception var7) {
  82.             System.out.println(STRING_Error_forming_URL);
  83.             return;
  84.          }
  85.  
  86.          MediaTracker tracker = new MediaTracker(this);
  87.          tracker.addImage(this.backgroundImage, 0);
  88.  
  89.          try {
  90.             tracker.waitForID(0);
  91.          } catch (InterruptedException var6) {
  92.             System.out.println(STRING_Image_loading_interrupted);
  93.          }
  94.       }
  95.  
  96.    }
  97.  
  98.    private int convertBGRtoRGB(int BGRColor) {
  99.       int r = (BGRColor & 255) << 16;
  100.       int g = BGRColor & '\uff00';
  101.       int b = (BGRColor & 16711680) >> 16;
  102.       return r + g + b;
  103.    }
  104.  
  105.    private String modifyStringContext(String param) {
  106.       if (param.startsWith(".")) {
  107.          param = param.replace('\\', '/');
  108.          String fullBase = ((Applet)this).getDocumentBase().toString();
  109.          param = fullBase.substring(0, fullBase.lastIndexOf(47)) + "/" + param;
  110.       } else if (param.startsWith("#")) {
  111.          param = ((Applet)this).getDocumentBase().toString() + param;
  112.       } else if (param.compareTo("javascript:void(0)") == 0) {
  113.          param = null;
  114.       } else if (!param.startsWith("cid:") && !param.startsWith("lifn:") && !param.startsWith("java:") && !param.startsWith("irc:") && !param.startsWith("IOR:") && !param.startsWith("ilu:") && !param.startsWith("https:") && !param.startsWith("http:") && !param.startsWith("hdl:") && !param.startsWith("gopher:") && !param.startsWith("ftp:") && !param.startsWith("finger:") && !param.startsWith("file:") && !param.startsWith("data:") && !param.startsWith("clsid:") && !param.startsWith("md5:") && !param.startsWith("mailserver:") && !param.startsWith("mailto:") && !param.startsWith("mid:") && !param.startsWith("news:") && !param.startsWith("nntp:") && !param.startsWith("path:") && !param.startsWith("prospero:") && !param.startsWith("service:") && !param.startsWith("shttp") && !param.startsWith("snews") && !param.startsWith("STANF:") && !param.startsWith("telnet:") && !param.startsWith("vemmi:") && !param.startsWith("wais:") && !param.startsWith("whois++:")) {
  115.          param = "http://" + param;
  116.       }
  117.  
  118.       return param;
  119.    }
  120.  
  121.    public void destroy() {
  122.    }
  123.  
  124.    private void displayImage(Graphics g) {
  125.       if (this.m_fAllLoaded) {
  126.          if (this.backgroundImage != null) {
  127.             int imHeight = this.backgroundImage.getHeight(this);
  128.             int imWidth = this.backgroundImage.getWidth(this);
  129.             Dimension d = ((Component)this).size();
  130.             Image theBuffer = ((Component)this).createImage(d.width, d.height);
  131.             Graphics gr = theBuffer.getGraphics();
  132.             int x = (d.width + this.xOffset) / imWidth + 1;
  133.             int y = (d.height + this.yOffset) / imHeight + 1;
  134.  
  135.             for(int i = 0; i < y; ++i) {
  136.                for(int j = 0; j < x; ++j) {
  137.                   gr.drawImage(this.backgroundImage, j * imWidth - this.xOffset, i * imHeight - this.yOffset, this);
  138.                }
  139.             }
  140.  
  141.             gr.drawImage(this.m_Images[this.m_nCurrImage], (((Component)this).size().width - this.m_Images[this.m_nCurrImage].getWidth(this)) / 2, (((Component)this).size().height - this.m_Images[this.m_nCurrImage].getHeight(this)) / 2, (ImageObserver)null);
  142.             g.drawImage(theBuffer, 0, 0, this);
  143.          } else {
  144.             g.clearRect(0, 0, ((Component)this).size().width, ((Component)this).size().height);
  145.             g.drawImage(this.m_Images[this.m_nCurrImage], (((Component)this).size().width - this.m_Images[this.m_nCurrImage].getWidth(this)) / 2, (((Component)this).size().height - this.m_Images[this.m_nCurrImage].getHeight(this)) / 2, (ImageObserver)null);
  146.          }
  147.  
  148.       }
  149.    }
  150.  
  151.    public void paint(Graphics g) {
  152.       if (this.m_fAllLoaded) {
  153.          Rectangle r = g.getClipRect();
  154.          g.clearRect(r.x, r.y, r.width, r.height);
  155.          this.displayImage(g);
  156.       } else if (this.NetworkFiles) {
  157.          g.drawString(STRING_Unable_to_load_images, 10, 20);
  158.       } else {
  159.          g.drawString(STRING_Loading_images, 10, 20);
  160.       }
  161.  
  162.    }
  163.  
  164.    public void start() {
  165.       if (this.m_RotatingPicture == null) {
  166.          this.m_RotatingPicture = new Thread(this);
  167.          this.m_RotatingPicture.start();
  168.       }
  169.  
  170.    }
  171.  
  172.    public void stop() {
  173.       if (this.m_RotatingPicture != null) {
  174.          this.m_RotatingPicture.stop();
  175.          this.m_RotatingPicture = null;
  176.       }
  177.  
  178.    }
  179.  
  180.    public void run() {
  181.       this.m_nCurrImage = 0;
  182.       if (!this.m_fAllLoaded) {
  183.          ((Component)this).repaint();
  184.          this.m_Graphics = ((Component)this).getGraphics();
  185.          this.m_Images = new Image[this.numImages];
  186.          MediaTracker tracker = new MediaTracker(this);
  187.  
  188.          for(int i = 1; i <= this.numImages; ++i) {
  189.             this.m_Images[i - 1] = ((Applet)this).getImage(((Applet)this).getDocumentBase(), this.imageNames[i - 1]);
  190.             tracker.addImage(this.m_Images[i - 1], 0);
  191.          }
  192.  
  193.          try {
  194.             tracker.waitForAll();
  195.             this.m_fAllLoaded = !tracker.isErrorAny();
  196.          } catch (InterruptedException var4) {
  197.          }
  198.  
  199.          if (!this.m_fAllLoaded) {
  200.             this.stop();
  201.             this.m_Graphics.drawString(STRING_Error_loading_images, 10, 40);
  202.             return;
  203.          }
  204.       }
  205.  
  206.       ((Component)this).repaint();
  207.  
  208.       while(true) {
  209.          try {
  210.             this.displayImage(this.m_Graphics);
  211.             Thread.sleep((long)this.pauseTime);
  212.             ++this.m_nCurrImage;
  213.             if (this.m_nCurrImage == this.numImages) {
  214.                this.m_nCurrImage = 0;
  215.             }
  216.          } catch (Exception var5) {
  217.             this.stop();
  218.          }
  219.       }
  220.    }
  221.  
  222.    public boolean mouseDown(Event evt, int x, int y) {
  223.       this.didClick = true;
  224.       return true;
  225.    }
  226.  
  227.    public boolean mouseUp(Event evt, int x, int y) {
  228.       if (this.didClick) {
  229.          try {
  230.             String target = ((Applet)this).getParameter("Target" + (this.m_nCurrImage + 1));
  231.             if (target != null) {
  232.                ((Applet)this).getAppletContext().showDocument(new URL(this.imageURLs[this.m_nCurrImage]), target);
  233.             } else {
  234.                ((Applet)this).getAppletContext().showDocument(new URL(this.imageURLs[this.m_nCurrImage]));
  235.             }
  236.          } catch (Exception var6) {
  237.             ((Applet)this).getAppletContext().showStatus(STRING_Bad_URL + this.imageURLs[this.m_nCurrImage]);
  238.          }
  239.       }
  240.  
  241.       return true;
  242.    }
  243.  
  244.    public boolean mouseEnter(Event evt, int x, int y) {
  245.       return true;
  246.    }
  247.  
  248.    public boolean mouseExit(Event evt, int x, int y) {
  249.       this.didClick = false;
  250.       return true;
  251.    }
  252. }
  253.